/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.market.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.market.entity.EventChannel;
import com.kaaction.modules.market.service.EventChannelService;

/**
 * 活动与渠道Controller
 * @author zengbiao
 * @version 2017-04-20
 */
@Controller
@RequestMapping(value = "${adminPath}/market/eventChannel")
public class EventChannelController extends BaseController {

	@Autowired
	private EventChannelService eventChannelService;
	
	@ModelAttribute
	public EventChannel get(@RequestParam(required=false) String id) {
		EventChannel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eventChannelService.get(id);
		}
		if (entity == null){
			entity = new EventChannel();
		}
		return entity;
	}
	
	@RequiresPermissions("market:eventChannel:view")
	@RequestMapping(value = {"list", ""})
	public String list(EventChannel eventChannel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EventChannel> page = eventChannelService.findPage(new Page<EventChannel>(request, response), eventChannel); 
		model.addAttribute("page", page);
		return "modules/market/eventChannelList";
	}
	
	@RequiresPermissions("market:eventChannel:view")
	@RequestMapping(value = {"detail"})
	public String detail(EventChannel eventChannel, HttpServletRequest request, HttpServletResponse response, Model model) {
		String eventId = request.getParameter("eventId");
		List<EventChannel> list = eventChannelService.findListByEventId(eventId); 
		model.addAttribute("list", list);
		return "modules/market/eventChannelDetailList";
	}

	@RequiresPermissions("market:eventChannel:view")
	@RequestMapping(value = "form")
	public String form(EventChannel eventChannel, Model model) {
		model.addAttribute("eventChannel", eventChannel);
		return "modules/market/eventChannelForm";
	}

	@RequiresPermissions("market:eventChannel:edit")
	@RequestMapping(value = "save")
	public String save(EventChannel eventChannel, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eventChannel)){
			return form(eventChannel, model);
		}
		eventChannelService.save(eventChannel);
		addMessage(redirectAttributes, "保存活动与渠道成功");
		return "redirect:"+Global.getAdminPath()+"/market/eventChannel/?repage";
	}
	
	@RequiresPermissions("market:eventChannel:edit")
	@RequestMapping(value = "delete")
	public String delete(EventChannel eventChannel, RedirectAttributes redirectAttributes) {
		eventChannelService.delete(eventChannel);
		addMessage(redirectAttributes, "删除活动与渠道成功");
		return "redirect:"+Global.getAdminPath()+"/market/eventChannel/?repage";
	}

}